package cn.iocoder.yudao.module.ent.controller.admin.systemtemplate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.ent.controller.admin.systemtemplate.vo.SystemTemplatePageReqVO;
import cn.iocoder.yudao.module.ent.controller.admin.systemtemplate.vo.SystemTemplateRespVO;
import cn.iocoder.yudao.module.ent.controller.admin.systemtemplate.vo.SystemTemplateSaveReqVO;
import cn.iocoder.yudao.module.ent.dal.dataobject.systemtemplate.SystemTemplateDO;
import cn.iocoder.yudao.module.ent.service.systemtemplate.SystemTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "企业大数据 - 系统查询模板")
@RestController
@RequestMapping("/ent/system-template")
@Validated
public class SystemTemplateController {

    @Resource
    private SystemTemplateService systemTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建系统查询模板")
    @PreAuthorize("@ss.hasPermission('ent:system-template:create')")
    public CommonResult<Long> createSystemTemplate(@Valid @RequestBody SystemTemplateSaveReqVO createReqVO) {
        return success(systemTemplateService.createSystemTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统查询模板")
    @PreAuthorize("@ss.hasPermission('ent:system-template:update')")
    public CommonResult<Boolean> updateSystemTemplate(@Valid @RequestBody SystemTemplateSaveReqVO updateReqVO) {
        systemTemplateService.updateSystemTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统查询模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ent:system-template:delete')")
    public CommonResult<Boolean> deleteSystemTemplate(@RequestParam("id") Long id) {
        systemTemplateService.deleteSystemTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统查询模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<SystemTemplateRespVO> getSystemTemplate(@RequestParam("id") Long id) {
        SystemTemplateDO systemTemplate = systemTemplateService.getSystemTemplate(id);
        return success(BeanUtils.toBean(systemTemplate, SystemTemplateRespVO.class));
    }


    @GetMapping("/simple-list")
    @Operation(summary = "获得系统查询模板")
    public CommonResult<List<SystemTemplateRespVO>> getSystemTemplateList() {
        SystemTemplatePageReqVO pageReqVO = new SystemTemplatePageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SystemTemplateDO> list = systemTemplateService.getSystemTemplatePage(pageReqVO).getList();
        return success(BeanUtils.toBean(list, SystemTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统查询模板分页")
    public CommonResult<PageResult<SystemTemplateRespVO>> getSystemTemplatePage(@Valid SystemTemplatePageReqVO pageReqVO) {
        PageResult<SystemTemplateDO> pageResult = systemTemplateService.getSystemTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SystemTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统查询模板 Excel")
    @PreAuthorize("@ss.hasPermission('ent:system-template:export')")
    @OperateLog(type = EXPORT)
    public void exportSystemTemplateExcel(@Valid SystemTemplatePageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SystemTemplateDO> list = systemTemplateService.getSystemTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统查询模板.xls", "数据", SystemTemplateRespVO.class,
                BeanUtils.toBean(list, SystemTemplateRespVO.class));
    }

}